from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSImmediateNoTarget as GSHandler
from GSResultsPolicies import GSMissionNoTarget as GSResults

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg
from CivcraftSCUtils import showNotEnoughHealthMsg

gc = CyGlobalContext()
localtext = CyTranslator()

class StimPack(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iUnitTypes = [ gc.getInfoTypeForString("UNIT_MARINE"), gc.getInfoTypeForString("UNIT_FIREBAT") ]
        self.m_iResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_STIM_PACK")
        self.m_iResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iResearchBuildingToBuildAt).getDescription()) )

        self.m_iStimCastDamage = 10
        self.m_iStimAttachment = gc.getInfoTypeForString("UNIT_ATTACHMENT_STIM_PACK")

    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        # For marines and firebats only
        if not pCastingUnit.getUnitType() in self.m_iUnitTypes:
            return False

        # Show this for all marines and firebats, only enable it if they have the research.
        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        bShowNotEnoughHealth = False
        eAfflictingPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if pUnit.currHitPoints() > 10:
                pUnit.doDamage(self.m_iStimCastDamage, eAfflictingPlayer)
                pUnit.attachUnitAttachment(eAfflictingPlayer, self.m_iStimAttachment)
            else:
                bShowNotEnoughHealth = True

        if bShowNotEnoughHealth:
            iPlayer = pCastingGroup.getOwner()
            showNotEnoughHealthMsg(iPlayer)

        # Don't continue the mission.
        return False


        

                        
            
        
            
            
